<#-- Validation for jquery.validate -->
<#-- Map validator to rule -->
<#assign app = "myproject">
<#assign FIELD_PREFIX = "obj.">
<#function mapRule V>
	<#assign r = "">
	<#if V.type == "required">
		<#assign r = "required : true">
	<#elseif V.type == "requiredstring">
		<#assign r = "required : true">
	<#elseif V.type == "stringlength">
		<#list V.params as p>
			<#assign r_add = "">
			<#if p.name == "minLength">
				<#assign r_add = "minlength : " + p.value>
			<#elseif p.name == "maxLength">
				<#assign r_add = "maxlength : " + p.value>
			</#if>
			<#if (r_add?length > 0)>
				<#assign r = r + r_add>
				<#if p_has_next>
					<#assign r = r + ", ">
				</#if>
			</#if>
		</#list>
		<#return r>
	<#elseif V.type == "int">
		<#assign r = "digits : true">
		<#list V.params as p>
			<#assign r_add = "">
			<#if p.name == "min">
				<#assign r_add = "min : " + p.value>
			<#elseif p.name == "max">
				<#assign r_add = "max : " + p.value>
			</#if>
			<#if (r_add?length > 0)>
				<#assign r = r + r_add>
				<#if p_has_next>
					<#assign r = r + ", ">
				</#if>
			</#if>
		</#list>
	<#elseif V.type == "double">
		<#assign r = "number : true">
		<#list V.params as p>
			<#if p.name == "min">
				<#assign r = r + ", " + "min : " + p.value>
			<#elseif p.name == "max">
				<#assign r = r + ", " + "max : " + p.value>
			</#if>
		</#list>
	<#elseif V.type == "date">
		<#assign r = "date : true">
	<#elseif V.type == "email">
		<#assign r = "emai : true">
	<#elseif V.type == "url">
		<#assign r = "url : true">
	</#if>
	<#return r>
</#function>

<#-- Map validator to message -->
<#function mapMessage V>
	<#assign m = "">
	<#if V.type == "required">
		<#assign m = "required : \"" + V.message + "\"">
	<#elseif V.type == "requiredstring">
		<#assign m = "required : ">
	<#elseif V.type == "stringlength">
		<#list V.params as p>
			<#assign m_add = "">
			<#if p.name == "minLength">
				<#assign m_add = "minlength : \"" + V.message + "\"">
			<#elseif p.name == "maxLength">
				<#assign m_add = "maxlength : \"" + V.message + "\"">
			</#if>
			<#if (m_add?length > 0)>
				<#assign m = m + m_add>
				<#if p_has_next>
					<#assign m = m + ", ">
				</#if>
			</#if>
		</#list>
	<#elseif V.type == "int">
		<#assign m = "digits : \"" + V.message + "\"">
		<#list V.params as p>
			<#assign m_add = "">
			<#if p.name == "minLength">
				<#assign m_add = "min : \"" + V.message + "\"">
			<#elseif p.name == "maxLength">
				<#assign m_add = "max : \"" + V.message + "\"">
			</#if>
			<#if (m_add?length > 0)>
				<#assign m = m + m_add>
				<#if p_has_next>
					<#assign m = m + ", ">
				</#if>
			</#if>
		</#list>
	<#elseif V.type == "double">
		<#assign m = "number : \"" + V.message + "\"">
		<#list V.params as p>
			<#assign m_add = "">
			<#if p.name == "minLength">
				<#assign m_add = "min : \"" + V.message + "\"">
			<#elseif p.name == "maxLength">
				<#assign m_add = "max : \"" + V.message + "\"">
			</#if>
			<#if (m_add?length > 0)>
				<#assign m = m + m_add>
				<#if p_has_next>
					<#assign m = m + ", ">
				</#if>
			</#if>
		</#list>
	<#elseif V.type == "date">
		<#assign m = "date : \"" + V.message + "\"">
	<#elseif V.type == "email">
		<#assign m = "emai : \"" + V.message + "\"">
	<#elseif V.type == "url">
		<#assign m = "url : \"" + V.message + "\"">
	</#if>
	<#return m>
</#function>

<#function getRule field>
	<#assign rs = "">
	<#list field.validators as V>
		<#assign rs = rs + mapRule(V)>
		<#if V_has_next>
			<#assign rs = rs + ", ">
		</#if>
	</#list>
	<#if (rs?length > 0)>
		<#return "\"" + FIELD_PREFIX + field.name + "\": {" + rs + "}">
	<#else>
		<#return "">
	</#if>
</#function>

<#function getMessage field>
	<#assign ms = "">
	<#list field.validators as V>
		<#assign ms = ms + mapMessage(V)>
		<#if V_has_next>
			<#assign ms = ms + ", ">
		</#if>
	</#list>
	<#if (ms?length > 0)>
		<#return "\"" + FIELD_PREFIX + field.name + "\": {" + ms + "}">
	<#else>
		<#return "">
	</#if>
</#function>

<#function getRules table>
	<#assign t_rs = "">
	<#list table.fields as field>
		<#assign t_r = getRule(field)>
		<#if (t_r?length > 0)>
			<#if (t_rs?length > 0)>
				<#assign t_rs = t_rs + ", " + t_r>
			<#else>
				<#assign t_rs = t_rs + t_r>
			</#if>
		</#if>
	</#list>
	<#return "{" + t_rs + "}">
</#function>

<#function getMessages table>
	<#assign t_ms = "">
	<#list table.fields as field>
		<#assign t_m = getMessage(field)>
		<#if (t_m?length > 0)>
			<#if (t_ms?length > 0)>
				<#assign t_ms = t_ms + ", " + t_m>
			<#else>
				<#assign t_ms = t_ms + t_m>
			</#if>
		</#if>
	</#list>
	<#return "{" + t_ms + "}">
</#function>

